package com.msmall.community.diary.action.html;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.album.model.Reply;
import com.msmall.community.diary.model.Diary;
import com.msmall.community.diary.model.DiaryReply;
import com.msmall.community.diary.service.DiaryManager;
import com.msmall.community.diary.vo.DiaryReplyVO;

public class DiaryAction extends HTMLBaseAction {

	private static final long serialVersionUID = 5378435257569352895L;

	private static final Log logger = LogFactory.getLog(DiaryAction.class);
	
	private boolean isOwner;
	
	private Long diaryid;
	
	private Long replyid;
	
	private String content;
	
	private Diary diary;
	private Diary preDiary;
	private Diary nextDiary;
	
	private DiaryReply reply;
	private List<DiaryReplyVO> replys;
	private DiaryManager diaryManager;

	/**
	 * 进入创建日记页面
	 */
	public String create() throws Exception{
		try{
			return SUCCESS;
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 保存日记
	 */
	public String save() throws Exception{
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		boolean tag = true;
		try{
			if(!getIsLogin()){
				message = getText("common.authentication.anonymous");
				tag = false;
			}
			if(tag){
				int validtitle = diaryManager.validateDiaryTitle(diary.getTitle());
				if(validtitle == 2){
					message = getText("html.space.diary.create.title.length.short");
					tag = false;
				}
				if(validtitle == 3){
					message = getText("html.space.diary.create.title.length.long");
					tag = false;
				}
				if(validtitle == 4){
					message = getText("html.space.diary.create.title.illegal");
					tag = false;
				}
			}
			if(tag){
				int validcontent = diaryManager.validateDiaryContent(diary.getContent());
				if(validcontent == 2){
					message = getText("html.space.diary.create.content.length.short");
					tag = false;
				}
				if(validcontent == 3){
					message = getText("html.space.diary.create.content.length.long");
					tag = false;
				}
				if(validcontent == 4){
					message = getText("html.space.diary.create.content.illegal");
					tag = false;
				}
			}
			if(!tag){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(message));
			}else{
				diary = diaryManager.saveDiary(diary, getUserid(), getUserAgentType());
				buffer.append(createResult("true"));
				buffer.append(createEasyContent(diary.getDiaryid().toString()));
			}
			buffer.append(ConstantAjax.RETURN_SUFF);
			message = buffer.toString();
			return SUCCESS;
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 删除日记
	 */
	public String remove() throws Exception{
		try{
			return SUCCESS;
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 分页显示某用户日记列表
	 */
	public String list() throws Exception{
		try{
			if(usr == null || usr == 0){
				usr = getUserid(); // 如果是登陆用户，跳转到自己的首页
				isOwner = true;
			}else if(usr.compareTo(getUserid()) == 0){
				isOwner = true;
			}else{
				isOwner = false;
			}
			
			diaryManager.diaryList(page, usr);
			if(isOwner){
				return "diaryOwner";
			}else{
				return "diaryViewer";
			}
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 验证日记标题/内容=
	 */
	public String valid() throws Exception{
		try{
			return SUCCESS;
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 查看日记，并显示回复
	 */
	public String view() throws Exception{
		try{
			diary = diaryManager.getDiaryByid(diaryid);
			preDiary = diaryManager.getDiaryPreByid(diaryid);
			nextDiary = diaryManager.getDiaryNextByid(diaryid);
			replys = diaryManager.getReplysByDiaryid(diaryid, ConstantDB.AVALIABLE);
			if(diary.getUserid().compareTo(getUserid()) == 0){
				isOwner = true;
			}else{
				isOwner = false;
			}
			if(isOwner){
				return "diaryViewOwner";
			}else{
				return "diaryViewViewer";
			}
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 保存日记回复
	 */
	public String saveReply() throws Exception{
		try{
			boolean success = true;
			StringBuffer buffer = new StringBuffer();
			buffer.append(ConstantAjax.RETURN_PRE);
			if(!getIsLogin()){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("common.authentication.anonymous")));
				success = false;
			}
			if((reply == null || reply.getContent() == null)&& success){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
				success = false;
			}
			if(success){
				int valid = diaryManager.validateReplyContent(reply.getContent());
				if(valid == 2){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
					success = false;
				}
				if(valid == 3){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.long")));
					success = false;
				}
				if(valid == 4){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.illegal")));
					success = false;
				}
			}
			if(success){
				reply = diaryManager.saveDiaryReply(reply, getUserid(), getUserAgentType());
				buffer.append(createResult("true"));
			}
			buffer.append(ConstantAjax.RETURN_SUFF);
			message = buffer.toString();
		}catch(Exception e){
			log.debug("============>", e);
		}
		return SUCCESS;
	}
	
	public Long getDiaryid() {
		return diaryid;
	}

	public void setDiaryid(Long diaryid) {
		this.diaryid = diaryid;
	}

	public Long getReplyid() {
		return replyid;
	}

	public void setReplyid(Long replyid) {
		this.replyid = replyid;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Diary getDiary() {
		return diary;
	}

	public void setDiary(Diary diary) {
		this.diary = diary;
	}

	public DiaryReply getReply() {
		return reply;
	}

	public void setReply(DiaryReply reply) {
		this.reply = reply;
	}

	public boolean getIsOwner() {
		return isOwner;
	}
	public void setIsOwner(boolean isOwner) {
		this.isOwner = isOwner;
	}
	public List<DiaryReplyVO> getReplys() {
		return replys;
	}
	public void setReplys(List<DiaryReplyVO> replys) {
		this.replys = replys;
	}
	public Diary getPreDiary() {
		return preDiary;
	}
	public void setPreDiary(Diary preDiary) {
		this.preDiary = preDiary;
	}
	public Diary getNextDiary() {
		return nextDiary;
	}
	public void setNextDiary(Diary nextDiary) {
		this.nextDiary = nextDiary;
	}
	public void setDiaryManager(DiaryManager diaryManager) {
		this.diaryManager = diaryManager;
	}
}
